/*
 *  Filename: systick.c
 *  Function: SysTick configuration
 *  Created: 2017-06-13
 *  Version: 1.0
 *  Author: Bin
 */

/*-------- Includes --------*/
#include "stm32f10x.h"
#include "systick.h"

/*-------- Includes --------*/
static uint32_t SysTick_Time = 0;

/*-------- Exported functions --------*/
void SysTick_Init(void)
{
    // Configure reload value for SysTick
    // Here period T = 1us
    if(SysTick_Config(SystemCoreClock / 1000000)) {
        while(1);
    }

    // Disable SysTick
    // SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;

    // Enable SysTick
    // SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
}

void delay_us(uint32_t nTime)
{
    SysTick_Time = nTime;

    while (SysTick_Time);
}

void delay_ms(uint32_t nTime)
{
    SysTick_Time = nTime * 1000;

    while (SysTick_Time);
}

/*-------- SysTick interrupt service routine --------*/
void SysTick_Handler(void)
{
    if (SysTick_Time)
        SysTick_Time--;
}
